package com.leo;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.StringConverter;

/**
 * @ClassName ComboBoxDemo
 * @Description
 * @Author wangss
 * @date 2020.05.25 11:45
 * @Version 1.0
 */
public class ComboBox2Demo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: #e7eeea");
        Scene scene = new Scene(anchorPane);

        Button button = new Button("点击");

        Student s1 = new Student("小a", 12, 99d);
        Student s2 = new Student("小b", 18, 63d);
        Student s3 = new Student("小c", 20, 60d);
        Student s4 = new Student("小d", 20, 60d);
        Student s5 = new Student("小e", 20, 60d);

        ComboBox<Student> comboBox = new ComboBox<>();
        comboBox.getItems().addAll(s1, s2, s3, s4, s5);

        // 可以编辑
        comboBox.setEditable(true);
        // 文本提示
        comboBox.setPromptText("请输入内容");

        // 这里只是负责数据转换
        comboBox.setConverter(new StringConverter<Student>() {
            @Override
            public String toString(Student object) {
                if (object == null) {
                    return "";
                }

                return object.getName();
            }

            @Override
            public Student fromString(String string) {
                return null;
            }
        });

        AnchorPane.setTopAnchor(comboBox, 10d);
        AnchorPane.setLeftAnchor(comboBox, 10d);
        AnchorPane.setTopAnchor(button, 10d);
        AnchorPane.setLeftAnchor(button, 260d);
        anchorPane.getChildren().addAll(comboBox, button);

        primaryStage.setScene(scene);
        primaryStage.setTitle("combobox");
        primaryStage.setWidth(625d);
        primaryStage.setHeight(500d);
        primaryStage.setResizable(false);
        primaryStage.show();

        // 这里负责展示效果
        comboBox.setCellFactory(new Callback<ListView<Student>, ListCell<Student>>() {
            @Override
            public ListCell<Student> call(ListView<Student> param) {

                return new ListCell<Student>() {
                    @Override
                    protected void updateItem(Student item, boolean empty) {
                        super.updateItem(item, empty);
                        if (!empty) {
                            // 获取数据
//                            comboBox.getConverter().toString(item);

                            HBox box = new HBox(5);
                            box.getChildren().addAll(new Label(item.getName()), new Label(String.valueOf(item.getAge())), new Label(String.valueOf(item.getScore())));
                            this.setGraphic(box);
                        }
                    }
                };
            }
        });
    }
}
